/*****************************************************************************
 * plum_fsm_attr.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "fsm.attr"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
#define FSM_ATTR_MALLOC(size) malloc(size)
#define FSM_ATTR_FREE(ptr)    free(ptr)
/*****************************************************************************
 * Private Types
 ****************************************************************************/
typedef struct {
    plum_u32         id;
    plum_void *      data;
    struct list_head list;
} fsm_atr_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_PRIVATE
fsm_atr_t atr_head = {
    .id        = 0,
    .data      = plum_null,
    .list.next = &atr_head.list,
    .list.prev = &atr_head.list,
};

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
fsm_atr_t *fsm_attr_list_find(plum_u32 id)
{
    struct list_head *pos;
    list_for_each(pos, &atr_head.list)
    {
        fsm_atr_t *item = list_entry(pos, fsm_atr_t, list);
        if (item->id == id) {
            return (item);
        }
    }

    return (plum_null);
}

PLUM_PRIVATE
fsm_atr_t *fsm_attr_list_register(plum_u32 id, plum_u32 size)
{
    fsm_atr_t *item = fsm_attr_list_find(id);
    if (item) {
        LOG_E("the id had registered");
        return (item);
    }

    fsm_atr_t *node = FSM_ATTR_MALLOC(sizeof(fsm_atr_t));
    if (!node) {
        LOG_E("malloc memery error");
        return (plum_null);
    }

    node->id   = id;
    node->data = FSM_ATTR_MALLOC(size);
    if (node->data) {
        LOG_E("malloc data err");
        FSM_ATTR_FREE(node);
        return (plum_null);
    }

    memset(node->data, 0, size);
    list_add(&node->list, &atr_head.list);

    return (node);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_cal_fsm_attr_read(plum_u32 atr_id, plum_void *arg)
{
    plum_s32 rc = PLUM_ECODE_EPARA;

    do {
        plum_u32 loop;
        plum_u32 count =
            PLUM_SECTION_ITEM_COUNT(PLUM_ATTR_SECTION, plum_fsm_atr_t);

        plum_fsm_atr_t *item;
        for (loop = 0; loop < count; loop++) {
            item =
                PLUM_SECTION_ITEM_GET(PLUM_ATTR_SECTION, plum_fsm_atr_t, loop);
            if (item->id == atr_id) {
                break;
            }
        }

        if (loop == count) {
            LOG_E("the attr id not register");
            break;
        }

        fsm_atr_t *node = fsm_attr_list_find(atr_id);
        if (!node) {
            LOG_E("the id not writen a value before");
            node = fsm_attr_list_register(atr_id, item->size);
            if (!node) {
                LOG_E("register attr list err");
                rc = PLUM_ECODE_EMEM;
                break;
            }
        }

        memcpy(arg, node->data, item->size);

    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_cal_fsm_attr_write(plum_u32 atr_id, plum_void *arg)
{
    plum_s32 rc = PLUM_ECODE_EPARA;

    do {
        plum_u32 loop;
        plum_u32 count =
            PLUM_SECTION_ITEM_COUNT(PLUM_ATTR_SECTION, plum_fsm_atr_t);

        plum_fsm_atr_t *item;
        for (loop = 0; loop < count; loop++) {
            item =
                PLUM_SECTION_ITEM_GET(PLUM_ATTR_SECTION, plum_fsm_atr_t, loop);
            if (item->id == atr_id) {
                break;
            }
        }

        if (loop == count) {
            LOG_E("the attr id not register");
            break;
        }

        fsm_atr_t *node = fsm_attr_list_find(atr_id);
        if (!node) {
            LOG_E("the id not writen a value before");
            node = fsm_attr_list_register(atr_id, item->size);
            if (!node) {
                LOG_E("register attr list err");
                rc = PLUM_ECODE_EMEM;
                break;
            }
        }

        memcpy(node->data, arg, item->size);

    } while (0);

    return (rc);
}

/*****************************************************************************
 * Cli Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_void fsm_cli_attr_list_all(plum_u32 argc, plum_s8 **argv)
{
    plum_u32 cnt = PLUM_SECTION_ITEM_COUNT(PLUM_ATTR_SECTION, plum_fsm_atr_t);
    LOG_A("have registerd %d attr", cnt);

    plum_u32 loop;

    for (loop = 0; loop < cnt; loop++) {
        plum_fsm_atr_t *item =
            PLUM_SECTION_ITEM_GET(PLUM_ATTR_SECTION, plum_fsm_atr_t, loop);
        plum_u8 *data = FSM_ATTR_MALLOC(item->size);
        plum_cal_fsm_attr_read(item->id, data);
        LOG_AHEX("attr: ", data, item->size);
    }
}
/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
